﻿using System;
using System.Collections.Specialized;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using VitCode.BaseDataPageCreator;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.ObjectResolutionHelper;
using VitCode.Web.UI;

namespace VitCode.Web.UserControls
{
    public partial class ObjHistoryListCtl : UserControl
    {
        private PageBuilder m_PageBuilder;

        public string ClassName
        {
            get { return hdnClassName.Value; }
            set { hdnClassName.Value = value; }
        }

        public string TypeName
        {
            get { return hdnTypeName.Value; }
            set { hdnTypeName.Value = value; }
        }

        public string KeyProVal
        {
            get { return hdnKeyProVal.Value; }
            set { hdnKeyProVal.Value = value; }
        }

        public string KeyProName
        {
            get { return hdnKeyProName.Value; }
            set { hdnKeyProName.Value = value; }
        }

        public string HiddenColInfo
        {
            get { return hdnHiddenColInfo.Value; }
            set { hdnHiddenColInfo.Value = value; }
        }

        public string SettingXmlPath
        {
            get { return AppDomain.CurrentDomain.BaseDirectory + "Setting\\HistoryObjProSet.xml"; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            pgList.DataSourceDelegate = new PaginationGrid.GridDataSourceDelegate(BindPgList);
        }

        public void BindData()
        {
            pgList.BindGrid();
        }

        public void PrintDataGrid()
        {
            DataTable dt = GetListData(" where 1=2 ");
            StringCollection sc = GetHiddenColNames();
            if (dt != null && dt.Columns.Count > 0)
            {
                var colCheck = new PaginationGridCheckColumn();
                colCheck.ColumnHeaderText = "版本比对";
                colCheck.DataField = dt.Columns[0].ColumnName;
                pgList.Columns.Add(colCheck);
                foreach (DataColumn col in dt.Columns)
                {
                    if(sc.Contains(col.ColumnName))
                        continue;
                    var boundCol = new PaginationGridBoundColumn(col.ColumnName, col.ColumnName);
                    if (col.DataType.Equals(typeof(DateTime)))
                        boundCol.DataFormatString = "{0:yyyy-MM-dd}";
                    pgList.Columns.Add(boundCol);
                }
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (m_PageBuilder == null)
                m_PageBuilder = new PageBuilder(Page, SettingXmlPath);
            //m_PageBuilder.SetPgListAttribute(pgList, ClassName, TypeName);
            base.OnInit(e);
        }

        private StringCollection GetHiddenColNames()
        {
            string[] hiddenColNames = HiddenColInfo.Split(',');
            StringCollection sc = new StringCollection();
            if (HiddenColInfo.Length > 0)
            {
                foreach (string str in hiddenColNames)
                {
                    if (!sc.Contains(str))
                        sc.Add(str);
                }
            }
            return sc;
        }

        private DataTable GetListData(string strCondition)
        {
            DataSet ds;
            var acc = new BaseDataAccess();
            
            var sqlHelper = new SqlHelper();
            m_PageBuilder.SetPgListAttribute(pgList, ClassName, TypeName);
            var sql = m_PageBuilder.GetPgListSqlValue(ClassName, TypeName);
            
            
            sql = sql.Replace("\r\n", " ").Replace("\t", " ");
            try
            {
                ds = sqlHelper.ExecuteDataSet(acc.Session, sql + strCondition);
            }
            finally
            {
                acc.CloseSession();
            }
            return ds.Tables[0];
        }

        private object BindPgList()
        {
            return GetListData(" where " + KeyProName + "='" + KeyProVal + "' ");
        }

        protected void pgList_OnGridShowData(object sender, PaginationGridShowDataArgs e)
        {
            const string HIS_PGLIST_DIFF_VAL_STYPE = "tdHisPgListDiffValStyle";
            int colIndex = e.ColumnIndex;
            string curVal = e.DataCell.Text;
            int rowIndex = e.RowIndex;
            int pevRowIndex = rowIndex - 1;
            if(pevRowIndex >= 0)
            {
                DataGridItem item = pgList.CurrentGrid.Items[pevRowIndex];
                TableCell pevCell = item.Cells[colIndex];
                string pevVal = pevCell.Text;
                if(!pevVal.Equals(curVal))
                {
                    e.DataCell.Attributes["class"] = HIS_PGLIST_DIFF_VAL_STYPE;
                    pevCell.Attributes["class"] = HIS_PGLIST_DIFF_VAL_STYPE;
                }
            }
            
        }
    }
}